#define _CRT_SECURE_NO_WARNINGS 1

//https://leetcode.cn/problems/zigzag-conversion/

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1)
            return s;

        int size = s.size();
        string ret;
        int d = 2 * (numRows - 1);
        for (int i = 0; i < numRows; i++)
        {
            if ((i == 0) || (i == numRows - 1))
            {
                int j = i;
                while (j < size)
                {
                    ret.push_back(s[j]);
                    j += d;
                }
            }
            else
            {
                int j = i;
                int k = d - j;
                while (j < size || k < size)
                {
                    if (j < size) ret.push_back(s[j]);
                    if (k < size) ret.push_back(s[k]);
                    j += d;
                    k += d;
                }
            }
        }
        return ret;
    }
};